﻿using Domain.Entitys.Pinv;
using Domain.IRepositorys.Pinv;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.Pinv
{
    public class VendorRepository : IVendorRepository
    {
        private readonly MyDbContext myDbContext;

        public VendorRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="vendor"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Pinv_VendorAdd(pinv_vendor vendor)
        {
            vendor.Del_flag = '0';
            await myDbContext.pinv_Vendor.AddAsync(vendor);
            return await myDbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 查询总条数
        /// </summary>
        /// <param name="Vendor_name"></param>
        /// <param name="Vendor_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Pinv_VendorCount(string Vendor_name, string Vendor_code)
        {
            //逻辑删除
            IQueryable<pinv_vendor> query = myDbContext.pinv_Vendor.Where(d => d.Del_flag == '0');
            if (!string.IsNullOrEmpty(Vendor_name))
            {
                query = myDbContext.pinv_Vendor.Where(d => d.Vendor_name.Contains(Vendor_name));
            }
            if (!string.IsNullOrEmpty(Vendor_code))
            {
                query = myDbContext.pinv_Vendor.Where(d => d.Vendor_code.Contains(Vendor_code));
            }
            return query.Count();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="vendor"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Pinv_VendorDelete(pinv_vendor vendor)
        {
            pinv_vendor pinv = await myDbContext.pinv_Vendor.FindAsync(vendor.Vendor_id);
            pinv.Del_flag = '1';
            return await myDbContext.SaveChangesAsync();

        }
        /// <summary>
        /// 分页、条件查询
        /// </summary>
        /// <param name="Vendor_name"></param>
        /// <param name="Vendor_code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<pinv_vendor> Pinv_VendorFYWhere(string Vendor_name, string Vendor_code, int currentPage, int pageSize)
        {
            // 逻辑删除条件
            IQueryable<pinv_vendor> query = myDbContext.pinv_Vendor.Where(d => d.Del_flag == '0');

            // 条件查询
            if (!string.IsNullOrEmpty(Vendor_name))
            {
                query = query.Where(d => d.Vendor_name.Contains(Vendor_name));
            }
            if (!string.IsNullOrEmpty(Vendor_code))
            {
                query = query.Where(d => d.Vendor_code.Contains(Vendor_code));
            }

            // 分页查询
            var data = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="vendor"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Pinv_VendorUpdate(pinv_vendor vendor)
        {
            pinv_vendor pinv = myDbContext.pinv_Vendor.Find(vendor.Vendor_id);
            pinv.Vendor_name = vendor.Vendor_name;
            pinv.Vendor_classify = vendor.Vendor_classify;
            pinv.Vendor_grade = vendor.Vendor_grade;
            pinv.Vendor_starttime = vendor.Vendor_starttime;
            pinv.Vendor_endtime = vendor.Vendor_endtime;
            pinv.Vendor_term = vendor.Vendor_term;
            pinv.Vendor_address = vendor.Vendor_address;
            pinv.Vendor_rep_id = vendor.Vendor_rep_id;
            pinv.Vendor_depd_id = vendor.Vendor_depd_id;
            pinv.Vendor_phone = vendor.Vendor_phone;
            pinv.Contacts_name = vendor.Contacts_name;
            pinv.Contacts_phone = vendor.Contacts_phone;
            pinv.Contacts_posts = vendor.Contacts_posts;
            pinv.Remark = vendor.Remark;
            pinv.Update_by = vendor.Update_by;
            pinv.Update_time = vendor.Update_time;
            myDbContext.pinv_Vendor.Update(pinv);
            return myDbContext.SaveChangesAsync();
        }

        public async Task<List<pinv_vendor>> Pinv_VendorWhere(string Vendor_name)
        {
            var query = myDbContext.pinv_Vendor.Where(d=>d.Del_flag=='0');
            if (!string.IsNullOrEmpty(Vendor_name))
            {
                query = query.Where(d => d.Vendor_name == Vendor_name || d.Vendor_code == Vendor_name);
            }
            return await query.ToListAsync();
        }
    }
}
